package com.glsc.ngateway.opmanage.controller.deposit;


import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.deposit.DepositoryDayProfit;
import com.glsc.ngateway.opmanage.aspect.UserCache;
import com.glsc.ngateway.opmanage.dto.DepositoryDayProfitExcelDto;
import com.glsc.ngateway.opmanage.dto.DepositoryDto;
import com.glsc.ngateway.opmanage.service.deposit.DepositoryDayProfitService;
import com.glsc.ngateway.opmanage.utils.report.ReportUploadContext;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 保证金管理-每日收益
 */
@RestController
@RequestMapping("/api/depositoryDayProfit")
@Slf4j
public class DepositoryDayProfitController {

    @Resource
    private UserCache userCache;

    @Resource
    private DepositoryDayProfitService depositoryDayProfitService;

    /**
     * 分页查找
     *
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("/page")
    public PlatformResponse<Page<DepositoryDayProfit>> pageTemplate(@RequestBody DepositoryDto dto, HttpServletRequest request) {

        Map<String, Object> param = new HashMap<>();
        if (StringUtils.isNotBlank(dto.getEffectDate())) {
            param.put("LIKE_effectDate", dto.getEffectDate());
        }
        Page<DepositoryDayProfit> page = depositoryDayProfitService.page(dto.getPageNo(), dto.getPageSize(), param);

        return PlatformResponse.successData(page);
    }

    /**
     * 根据id查找
     *
     * @param
     * @return
     */
    @GetMapping("/findById")
    public PlatformResponse<DepositoryDayProfit> findTemplateById(@ApiParam(required = false, value = "id") @RequestParam(required = true) Integer id) {
        return PlatformResponse.successData(depositoryDayProfitService.findById(id));
    }

    /**
     * 保证金收益，今日合计
     *
     * @return
     */
    @GetMapping("/thisProfit")
    public PlatformResponse<DepositoryDayProfit> thisProfit() {
        return PlatformResponse.successData(depositoryDayProfitService.thisProfit());
    }

    /**
     * 保存
     *
     * @param obj
     * @return
     */
    @PostMapping("/save")
    public PlatformResponse<?> saveTemplate(@RequestBody DepositoryDayProfit obj) {
        depositoryDayProfitService.save(obj);
        return PlatformResponse.success();
    }

    /**
     * 根据id删除
     *
     * @param obj
     * @return
     */
    @PostMapping("/delete")
    public PlatformResponse<?> deleteTemplate(@RequestBody DepositoryDayProfit obj) {
        depositoryDayProfitService.delete(obj.getId());
        return PlatformResponse.success();
    }


    /**
     * 数据导入
     *
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/import")
    @ApiOperation(value = "数据导入", notes = "数据导入")
    public PlatformResponse<?> outSystemImport(@RequestPart("file") MultipartFile file) throws IOException {
        ReportUploadContext<DepositoryDayProfitExcelDto, String> context = depositoryDayProfitService.important(file);
        return context.isCheckPass() ? PlatformResponse.successData(context.getReadRes()) : PlatformResponse.failedMsg(String.join(",", context.getCheckRes()));
    }


}
